package com.example.administrator.szb.util;

import android.content.Context;
import android.content.res.AssetManager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Set;

/**
 * TODO<读取Json文件的工具类>
 *
 * @author: 小嵩
 * @date: 2017/3/16 16:22

 */

public class GetJsonDataUtil {


    public String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    public static String simpleMapToJsonStr(Map map) {
        if (map == null || map.isEmpty()) {
                return "null";

        }

        String jsonStr = "{";
        Set keySet = map.keySet();

            for (Object key : keySet) {

            jsonStr += "\"" + key + "\":\"" + map.get(key) + "\",";

            }

        jsonStr = jsonStr.substring(0, jsonStr.length() - 2);

        jsonStr += "\"}";

        return jsonStr;

 }


}

